//
//  ZFBBusinessHeaderBottomView.m
//  01-ZFB
//
//  Created by My Mac on 2017/6/4.
//  Copyright © 2017年 MyMac. All rights reserved.
//

#import "ZFBBusinessHeaderBottomView.h"

@interface ZFBBusinessHeaderBottomView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong) NSArray *arr;

@end

@implementation ZFBBusinessHeaderBottomView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    return self;
}

-(void) setUpUI
{
    _arr = @[@"business_ad_001",@"business_ad_002",@"business_ad_003",@"business_ad_004"];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    NSInteger itemCount = 2;
    CGSize size = [UIScreen mainScreen].bounds.size;
    CGFloat margin = 3;
    CGFloat itemWidth = ( size.width - (itemCount -1) * margin ) / itemCount;
    flowLayout.itemSize = CGSizeMake(itemWidth - itemCount * margin, 100);
    flowLayout.minimumLineSpacing = margin;
    flowLayout.minimumInteritemSpacing = margin;
    flowLayout.sectionInset = UIEdgeInsetsMake(margin, margin, 0, margin);
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.frame = self.bounds;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.bounces = NO;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myItem"];
    collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:collectionView];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myItem" forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:cell.bounds];
    NSString *imageName = _arr[indexPath.item];
    imageView.image = [UIImage imageNamed:imageName];
    [cell addSubview:imageView];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(businessHeaderBottomView:atIndex:)]) {
        [self.delegate businessHeaderBottomView:self atIndex:indexPath.item];
    }
}

@end
